#!/usr/bin/env node

import { JavaDecompiler } from './dist/decompiler.js';
import { ClassFinder } from './dist/class-finder.js';
import * as fs from 'fs/promises';

async function testDirectDecompilation() {
    console.log('🧪 直接测试CFR反编译功能');
    console.log('='.repeat(60));
    
    const decompiler = new JavaDecompiler();
    const classFinder = new ClassFinder();
    
    // 读取本地class文件
    const classPath = '/home/hxz/cc/decom/io/choerodon/mybatis/domain/AuditDomain.class';
    
    try {
        const classBytes = await fs.readFile(classPath);
        console.log(`📁 读取类文件: ${classPath}`);
        console.log(`📦 文件大小: ${classBytes.length} bytes`);
        
        console.log('\n🔧 开始CFR反编译...');
        const result = await decompiler.decompileClass(classBytes, 'io.choerodon.mybatis.domain.AuditDomain');
        
        if (result.success) {
            console.log('✅ 反编译成功！');
            console.log('\n' + '='.repeat(80));
            console.log('📄 反编译结果:');
            console.log('='.repeat(80));
            console.log(result.source);
            console.log('='.repeat(80));
            
            // 检查是否包含方法体
            const hasMethodBody = result.source.includes('if (this._innerMap == null)');
            const hasExceptionHandling = result.source.includes('catch (Exception');
            const hasCompleteLogic = result.source.includes('this._innerMap.put(key, value)');
            
            console.log('\n🔍 代码完整性检查:');
            console.log(`  - 包含方法体逻辑: ${hasMethodBody ? '✅' : '❌'}`);
            console.log(`  - 包含异常处理: ${hasExceptionHandling ? '✅' : '❌'}`); 
            console.log(`  - 包含完整逻辑: ${hasCompleteLogic ? '✅' : '❌'}`);
            
            if (hasMethodBody && hasExceptionHandling && hasCompleteLogic) {
                console.log('\n🎉 反编译功能完全正常！已输出完整的Java代码逻辑。');
            } else {
                console.log('\n⚠️  反编译功能仍有问题，未输出完整逻辑。');
            }
            
        } else {
            console.log('❌ 反编译失败:', result.error);
        }
        
    } catch (error) {
        console.error('❌ 测试失败:', error);
    }
}

testDirectDecompilation().catch(console.error);